package cn.edu.sbs.talent.result;

import lombok.Data;
import lombok.NonNull;
import org.apache.shiro.util.StringUtils;

@Data
public class Result {

    @NonNull private int code;
    @NonNull private String msg;
    private Object data;

    private Result(@NonNull ResultCode code, String msg, Object data) {
        this.code = code.getCode();
        this.msg = StringUtils.hasLength(msg) ? msg : code.getDefMsg();
        this.data = data;
    }

    public static Result build(@NonNull ResultCode code, String msg, Object data) {
        return new Result(code, msg, data);
    }

    public static Result build(@NonNull ResultCode code, String msg) {
        return new Result(code, msg, null);
    }

    public static Result build(@NonNull ResultCode code) {
        return new Result(code, null, null);
    }

    public static Result success(Object data) {
        return build(ResultCode.SUCCESS, null, data);
    }

    public static Result fail(String msg) {
        return build(ResultCode.FAIL, msg);
    }

    public static Result unauthorized(String msg) {
        return build(ResultCode.UNAUTHORIZED, msg);
    }

    public static Result unknown(String msg) {
        return build(ResultCode.NOT_FOUND, msg);
    }

    public static Result error(String msg) {
        return build(ResultCode.NOT_FOUND, msg);
    }

}
